package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.web.controller.system.BaseController;
import cn.itcast.web.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {
  @Reference
  private ExtCproductService extCproductService;
  @Reference
  private FactoryService factoryService;
  @Autowired
  private FileUploadUtil fileUploadUtil;

  /*
   url：/cargo/extCproduct/list.do
   参数： contractId 合同的id , contractProductId 货物的id
   返回值：  "cargo/extc/extc-list";  /
   */
  @RequestMapping("/list")
  public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize,
                     String contractId, String contractProductId) {
    //1. 附件的列表页面
    ExtCproductExample extCproductExample = new ExtCproductExample();
    extCproductExample.createCriteria().andContractProductIdEqualTo(contractProductId);
    PageInfo<ExtCproduct> pageInfo = extCproductService.findByPage(extCproductExample, pageNum, pageSize);
    request.setAttribute("pageInfo", pageInfo);
    //2. 生产附件的生产厂家
    FactoryExample factoryExample = new FactoryExample();
    factoryExample.createCriteria().andCtypeEqualTo("附件");
    List<Factory> factoryList = factoryService.findAll(factoryExample);
    request.setAttribute("factoryList", factoryList);
    //3. 合同的id
    request.setAttribute("contractId", contractId);
    //4. 货物的id
    request.setAttribute("contractProductId", contractProductId);
    return "cargo/extc/extc-list";  //请求转发
  }

  /*
     实现保存与更新附件
     url: /cargo/extCproduct/edit.do
     参数：ExtCproduct
     返回值： 重定向到list.do页面
*/
  @RequestMapping("/edit")
  public String edit(ExtCproduct extCproduct, MultipartFile productPhoto) throws Exception {
    //由于后期需要使用到某些数据，所以我们需要补全数据
    //货物的所属公司的id
    extCproduct.setCompanyId(getLoginCompanyId());
    //货物的所属公司的名字
    extCproduct.setCompanyName(getLoginCompanyName());
    //货物创建人
    extCproduct.setCreateBy(getLoginUser().getId());
    //货物创建人所属的部门
    extCproduct.setCreateDept(getLoginUser().getDeptId());
    //七牛文件上传
    if (productPhoto != null) {
      String url = fileUploadUtil.upload(productPhoto);//返回的就是上传图片到地址
      extCproduct.setProductImage("http://" + url);//路径并没有存储http的，所以前面添加http
    }

    if (StringUtil.isEmpty(extCproduct.getId())) {
      //保存
      extCproductService.save(extCproduct);
    } else {
      //更新
      extCproductService.update(extCproduct);
    }
    //返回值一旦添加redirect或者是forward，那么则没有经过视图解析器。
    return "redirect:/cargo/extCproduct/list.do?contractId=" + extCproduct.getContractId() + "&contractProductId=" + extCproduct.getContractProductId();
  }

  /*
     进入更新附件的页面
     url: /cargo/contract/toUpdate.do
     参数：id  附件的id ,  contractId  合同的id， contractProductId 货物的id
     返回值：cargo/extc/extc-update
*/
  @RequestMapping("/toUpdate")
  public String toUpdate(String id) {
    //1.找到生产厂家
    FactoryExample factoryExample = new FactoryExample();
    factoryExample.createCriteria().andCtypeEqualTo("附件");
    List<Factory> factoryList = factoryService.findAll(factoryExample);
    request.setAttribute("factoryList", factoryList);
    //2.附件的对象
    ExtCproduct extCproduct = extCproductService.findById(id);
    request.setAttribute("extCproduct", extCproduct);
    //3. 合同的id
    request.setAttribute("contractId", extCproduct.getContractId());
    //4.  货物的id
    request.setAttribute("contractProductId", extCproduct.getContractProductId());
    return "cargo/extc/extc-update";
  }

  /*
     删除附件
     url: http://localhost:8080/cargo/extCproduct/delete.do
     参数：id  附件的id ,  contractId  合同的id， contractProductId 货物的id
     返回值：list.do
*/
  @RequestMapping("/delete")
  public String delete(String id, String contractId, String contractProductId) {
    extCproductService.delete(id);
    return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
  }
}
